package sd.czw.com.mmgapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.OkHttpUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.model.loginandsign.CodeCallback;
import sd.czw.com.mmgapp.model.loginandsign.CodeModel;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/15.
 */
public class CodeActivity extends AppCompatActivity {

    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_fx)
    TextView tvFx;
    @Bind(R.id.tv)
    TextView tv;
    @Bind(R.id.edt_numbers)
    EditText edtNumbers;
    @Bind(R.id.getphonenumber)
    Button getphonenumber;
    private String phonenumber;
    private String code;
    private String codes;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_code);
        ButterKnife.bind(this);
        phonenumber = getIntent().getStringExtra("phonenumber");
        tvFx.setText("我们已经给你的手机"+phonenumber+"发送了一条验证短信");
    }

    @OnTextChanged(value = R.id.edt_numbers,callback = OnTextChanged.Callback.TEXT_CHANGED)
    void onTextChanged(CharSequence s, int start, int before, int count) {
        codes = edtNumbers.getText().toString();
        if (codes != null && !("".equals(codes))){
            getphonenumber.setBackground(getDrawable(R.drawable.login_checkbtn));
        }else {
            getphonenumber.setBackground(getDrawable(R.drawable.login_btn));
        }
    }

    @OnClick({R.id.rl_back,R.id.getphonenumber})
    void ovclick(View view){
        switch (view.getId()){
            case R.id.rl_back:
                onBackPressed();
                break;
            case R.id.getphonenumber:
               //startActivity(new Intent(this,ResetPasswordActivity.class));
                final Intent intent = new Intent(CodeActivity.this, ResetPasswordActivity.class);
                code = edtNumbers.getText().toString().trim();
                OkHttpUtils.post().url(Contants.NMLOGIN).addParams("time",Contants.Time)
                        .addParams("sign",Contants.Sign).addParams("code",code).addParams("act","check_code")
                        .addParams("mobile_phone",phonenumber).build()
                        .execute(new CodeCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {

                            }

                            @Override
                            public void onResponse(CodeModel response, int id) {
                                if (response.getErrcode() == 1){
                                    intent.putExtra("phonenumber",phonenumber);
                                    startActivity(intent);
                                }else {
                                    Toast.makeText(CodeActivity.this, ""+response.getErrmsg(), Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                break;
        }
    }
}
